$(function () {

    var result = true;
    //新增
    $("[name='btn_save_new_user']").click(function () {
        var form = window.form_new_user;
        var account = form.account.value;
        var pwd = form.pwd.value;
        var confirmPwd = form.confirmPwd.value;
        var realName = form.realName.value;
        var depotId = form.depotId.value;
        var sta = form.sta.value;
        var nodes = "";
        $('input[name="nodes"]:checked').each(function(){
            nodes += $(this).val()+",";
        })
        var lgsDepot = {account: account, pwd: pwd, confirmPwd: confirmPwd, realName: realName, depotId: depotId,nodes: nodes, sta: sta };

        if(result){
            // result = false;
            $.postJsonBody("/pdaUser/insert", lgsDepot, function (re) {
                window.location.href=window.location.href;
                window.location.reload;
                // result = true;
            })
        }

    });

    //编辑
    $("[name='btn_save_edit_user']").click(function () {
        var form = window.form_edit_user;
        var id = form.id.value;
        var pwd = form.pwd.value;
        var confirmPwd = form.confirmPwd.value;
        var sta = form.sta.value;
        var nodes = "";
        $('input[name="editNodes"]:checked').each(function(){
            nodes += $(this).val()+",";
        })
        var depot = {id: id, pwd: pwd,confirmPwd: confirmPwd,nodes: nodes, sta: sta};

        $.postJsonBody("/pdaUser/update" , depot, function (re) {

            window.location.href=window.location.href;
            window.location.reload;
        })
    });

    // 编辑详情
    $('#md_edit_user').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("user-id");
        var modal = $(this);
        var form = window.form_edit_user;
        $.get("/pdaUser/getUser/" + id, function (re) {
            var user = re.data.lgsPdaUser;
            var nodeList = re.data.nodeList;
            form.id.value = user.id;
            form.account.value = user.account;
            form.realName.value = user.realName;
            form.depotName.value = user.depotName;
            form.sta.value = user.sta;
            $("[name='editNodes']").prop("checked", false);
            for(var i=0;i<nodeList.length;i++){
                $(".nodes_"+nodeList[i].nodeId).prop("checked", true);
            }


        });
    });


    //数据导出
    $("[name='btn_export']").click(function () {
        var keyword = $("#keyword").val();
        var sta = $("#sta").val();
        var depotName = $("#depotName").val();
        var realName = $("#realName").val();
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
            bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                    var url="/pdaUser/export?keyword="+keyword+"&sta="+sta+"&depotName="+depotName+"&realName="+realName;
                    window.open(url);
                }
            });
        }else{
            bootbox.balert("没有数据，无法导出!");
        }
    });
})